package top.huhuiyu.spirngboot2.total.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.huhuiyu.spirngboot2.total.base.BasePageResult;
import top.huhuiyu.spirngboot2.total.base.BaseResult;
import top.huhuiyu.spirngboot2.total.base.PageBean;
import top.huhuiyu.spirngboot2.total.entity.message.TbMessage;
import top.huhuiyu.spirngboot2.total.service.message.TbMessageService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {
    private final TbMessageService tbMessageService;

    @PostMapping("")
    public BaseResult add(@RequestBody TbMessage tbMessage) {
        return tbMessageService.add(tbMessage);
    }

    @GetMapping("")
    public BasePageResult<List<TbMessage>> query(TbMessage message, PageBean pageBean, Integer orderBy) {
        orderBy = Optional.ofNullable(orderBy).orElse(1);
        return tbMessageService.query(pageBean, message, orderBy);
    }

    @GetMapping("/{id}")
    public BasePageResult<TbMessage> query(@PathVariable("id") Integer id, PageBean pageBean) {
        return tbMessageService.queryByKey(id, pageBean);
    }
}
